package com.rtmanager.ui;



import com.rtmanager.R;
import com.rtmanager.data.RTCommunicator;
import com.rtmanager.data.Ticket;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class UI extends Activity {
	public String login;
	private String password;
	private String server;
	private TextView test;
	private boolean existsConfig=false;
	private ListView lv;
	private ProgressDialog pgd;
	private Context mContext;
	private Ticket[] listTicket;

	
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			lv.setAdapter(new ListTicketAdapter(mContext, listTicket));
			pgd.dismiss();
		}

		
	};
	

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	mContext=getApplicationContext();
    	try {
    		showSetup();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	/* TODO ajouter une condition pour voir si les paramètres sont enregistrés */

    }

    /**
     * affiche le formulaire de configuration
     * @return
     * @throws Exception
     */
    private boolean showSetup() {
    	try {
	    	setContentView(R.layout.setup_dialog);
			((EditText) findViewById(R.id.inServer)).setText(R.string.server);
			
	
	    	Button buttonOK=(Button)findViewById(R.id.validate);

	    	buttonOK.setOnClickListener(new View.OnClickListener() {
				
				public void onClick(View v) {
					// TODO Auto-generated method stub

			    	try {				    					    	  
	    	    		savConfig();
						showTicketList(v);
			    	}
				    catch(Exception e) {
				    	e.printStackTrace();
				    }
					
				}
			});
	    	return true;
    	}
	    catch (Exception e) {
	    	e.printStackTrace();
	    	return false;
	    }
	    
    }
    
    private void showError(Context context, Exception e) {
    	test= new TextView(context);
    	test.setText("erreur dans onCreate après dialog" + e.toString()+" \n"+e.getMessage()+"\n\n"+e.getLocalizedMessage());
    	setContentView(test);
    }
    
    
    /**
     * sauvegarde de la configuration utilisateur
     */
    /* TODO prévoir une sauvegarde des paramètres */
    private void savConfig() {
		login = ((EditText)findViewById(R.id.inLogin)).getText().toString();
		password = ((EditText) findViewById(R.id.inPwd)).getText().toString();
		server = ((EditText) findViewById(R.id.inServer)).getText().toString();
    }
    
    /**
     * Affichage de la liste des tickets
     * @throws Exception 
     */
    private void showTicketList (final View v) throws Exception {
    	String TAG = this.getClass().getCanonicalName() +  "showTicketList";
    	
    	lv=new ListView(v.getContext());
    	pgd = ProgressDialog.show(this, "chargement", "Chargement des tickets");    	
    	setContentView(lv);
	    
	    
    	try {
    		new Thread( new Runnable() {
    			
    			public void run() {
    				String TAG = this.getClass().getCanonicalName() +  "showTIcketlist:Thread";
    				Log.d(TAG, "server :" + server + ", login :"+login  );
		    		RTCommunicator testServer = new RTCommunicator(server,login, password);
			    	pgd.setMessage("chargement des tickets...");
					try {
						listTicket = testServer.getOwnerOpen();
						Log.d(TAG,"listTIcket >> : " + listTicket);
						handler.sendEmptyMessage(0);
				    	
						
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
						
					
		
					pgd.dismiss();
	    		}
    		}).start();
    	}
    	catch (Exception e) {
    		e.printStackTrace();
    	}
    }
    
}
